﻿using CefSharp;
using CefSharp.WinForms;
using System.Windows.Forms;

namespace CefDemo.cef
{
    public class BrowserFactory
    {
        public static ChromiumWebBrowser InitWebViewWithUrl(string url)
        {
            var browser = new ChromiumWebBrowser(url);
            browser.LifeSpanHandler = new DefaultLifeSpanHandler();
            browser.MenuHandler = new DefaultMenuHandler();
            browser.Dock = DockStyle.Fill;
            var browserSettings = new BrowserSettings();
            browserSettings.FileAccessFromFileUrls = CefState.Enabled;
            browserSettings.UniversalAccessFromFileUrls = CefState.Enabled;
            //禁用跨域
            browserSettings.WebSecurity = CefState.Disabled;
            browser.BrowserSettings = browserSettings;
            browser.JavascriptObjectRepository.Settings.LegacyBindingEnabled = true;
            return browser;
        }

        public static ChromiumWebBrowser InitWebViewWithLocal(string path)
        {
            var page = string.Format(@"{0}\{1}", Application.StartupPath, path);
            return InitWebViewWithUrl(page);
        }

        public static ChromiumWebBrowser InitWebViewWithEmbeddedResource(string path)
        {
            var page = string.Format(@"{0}://{1}", ResourceSchemeHandlerFactory.SchemeName, path);
            return InitWebViewWithUrl(page);
        }
    }
}
